<?php
/**
 * Created by PhpStorm.
 * User: godjarvis
 * Date: 2020/08/05
 * Time: 16:13
 */

namespace App\Http\Controllers;

use App\Bucket\Aspect\SearchLogAspect;
use App\Http\Requests\Product\IndexRequest;
use App\Http\Requests\Product\InfoRequest;
use App\Http\Requests\Product\QRCodeRequest;
use App\Http\Requests\Product\SearchFlushRequest;
use App\Http\Requests\Product\SharePreviewRequest;
use App\Http\Requests\Product\ShareRequest;
use App\Model\Service\DangKou\ProductService;
use Fresh\Annotation\Authorize;
use Fresh\Annotation\Autowired;
use Fresh\Annotation\RequestMapping;
use Fresh\Mvc\Controller;

/**
 * @Authorize()
 */
class ProductController extends Controller
{
    /**
     * @Autowired()
     * @var ProductService
     */
    public $productService;

    /**
     * 商品预加载
     * @RequestMapping(value="/product/preview")
     */
    public function previewAction()
    {
        return [
            'filter' => $this->productService->getFrontProps(),
        ];
    }

    /**
     * 商品列表
     * @RequestMapping(value="/product/list",method="POST")
     * @SearchLogAspect()
     */
    public function listAction(IndexRequest $request)
    {
        return $this->productService->list(
            [
                'keywords' => $request->keywords,
                'filter'   => $request->filter,
            ],
            $request->page,
            $request->limit
        );
    }

    /**
     * 商品详情
     * @RequestMapping(value="/product/info",method="POST")
     */
    public function infoAction(InfoRequest $request)
    {
        return $this->productService->getInfo($request->pnid, $request->pid);
    }

    /**
     * 商品搜索历史
     * @RequestMapping(value="/product/search/history")
     */
    public function historyAction()
    {
        return $this->productService->history();
    }

    /**
     * 删除商品搜索历史
     * @RequestMapping(value="/product/search/flush")
     * @throws \Fresh\Mvc\Model\Service\Exception
     */
    public function flushAction(SearchFlushRequest $request)
    {
        return $this->productService->flush($request->ids);
    }

    /**
     * 分享
     * @RequestMapping(value="/product/share")
     */
    public function shareAction(ShareRequest $request)
    {
        return $this->productService->share($request->pnid, $request->pid, $request->type, $request->location);
    }

    /**
     * 分享连接地址
     * @RequestMapping(value="/product/share/preview")
     */
    public function sharePreviewAction(SharePreviewRequest $request)
    {
        return $this->productService->preview($request->type, $request->pnid, $request->pid);
    }

    /**
     * 生成图片二维码
     * @RequestMapping(value="/product/qrcode")
     */
    public function QRCodeAction(QRCodeRequest $request)
    {
        return $this->productService->getQRCode($request->pnid, $request->pid);
    }
}
